from plone.app.layout.viewlets import common as base
from zope.component import getMultiAdapter
from cgi import escape
from Products.CMFPlone.utils import safe_unicode
from Products.CMFCore.utils import getToolByName

class TitleViewlet(base.ViewletBase):

    def update(self):
        portal_state = getMultiAdapter((self.context, self.request),
                                        name=u'plone_portal_state')
        context_state = getMultiAdapter((self.context, self.request),
                                         name=u'plone_context_state')
        page_title = escape(safe_unicode(context_state.object_title()))



        titulo=getToolByName(self.context,"portal_url").getPortalObject().Title
        portal_title = escape(safe_unicode(titulo()))


        if page_title == portal_title:
            self.site_title = portal_title
        else:
            self.site_title = u"%s | %s" % (page_title,portal_title)

    def index(self):
        return "<title>%s</title>" %self.site_title